#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    detectarLineas.py
#       
#    Copyright 2010 Victor Ramirez <virako.9@gmail.com>
#       
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.



from opencv.highgui import *
from opencv.cv import *
from opencv import *
from math import *


def detectarLineas(image):

    # It's not magic, but almost xD
    # cargamos una imagen y creamos una matriz
    img = cvLoadImageM(image, CV_LOAD_IMAGE_GRAYSCALE)
    eig_image = cvCreateMat(img.rows, img.cols, CV_32FC1)
    temp_image = cvCreateMat(img.rows, img.cols, CV_32FC1)

    #Creamos imágenes auxiliares para utilizar filtros (Canny y Smooth)
    img3 = cvCloneImage(img)
    img4 = cvCloneImage(img)
    img5 = cvCloneImage(img)
    cvCanny(img3, img4, 50,200,3)
    cvSmooth(img4,img5,CV_GAUSSIAN,5,5)


    storage = cvCreateMemStorage(0)

    # buscamos lineas en la imagen que hemos filtrado anteriormente
    lines = cvHoughLines2( img5, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 100, 0, 0 )
    
    #devuelve una matriz con puntos
    return lines
    
    """
    color = CvScalar()
    color[0] = 255
    color[3] = 255

    # recorremos las lineas y las pintamos
    #for line in lines:
    #    cvLine(img, line[1], line[1], 3, 8)
    print lines[1][2], lines[1][3] #cvLine(img, lines[0][0], lines[0][1], 3, 8)
    print lines[2][2], lines[2][3] #cvLine(img, lines[1][0], lines[1][1], 3, 8)
    
    cvLine(img, lines[2][0], lines[2][1], 3, 8)
    cvLine(img, lines[3][0], lines[3][1], 3, 8)
    
    print lines[0][2], lines[0][3] #cvLine(img, lines[0][2], lines[0][3], 3, 8) 
    print lines[1][2], lines[1][3] #cvLine(img, lines[1][2], lines[1][3], 3, 8)
    
    cvShowImage( "Hough", img )
    cvWaitKey(0)


    #Crear una ventana
    cvNamedWindow("Prueba1", 0)

    #mostrar imagen en ventana
    cvShowImage("Prueba1",img)

    #Espera pulsacion de teclado
    cvWaitKey(0)
    
    return lines
    """


lin = detectarLineas("f1.jpg")







